//
//  HomeWebviewLoadingController.m
//  New_Runway
//
//  Created by mack on 2018/1/25.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "HomeWebviewLoadingController.h"


@interface HomeWebviewLoadingController ()<UIActionSheetDelegate>

@end

@implementation HomeWebviewLoadingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = self.mainTitle;
    
    if ([self.urlstr isEqualToString:@""]) {
        [self.view addDefaultViewWithText:@"暂无证书内容" defaultImageType:DefaultImageTypeNoData];
        
    }else{
        [self loadHTML:self.urlstr];
    }
    [self.view bringSubviewToFront:self.navigationBarView];
}
-(void)rightItemClick{

    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"微信",@"朋友圈", nil ];
    [sheet showInView:self.view];
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self shareToWechat:0];
    }else if (buttonIndex == 1){
        [self shareToWechat:1];
    }
    
}

-(void)shareToWechat:(NSInteger)tag{

    NSString *url;
    if ([self.currUrl containsString:@"carId"]) {
        NSArray *array = [self.currUrl componentsSeparatedByString:@"carId="]; //从字符A中分隔成2个元素的数组
        NSString *carId = array[1];
        url = [NSString stringWithFormat:@"%@/controller/wx/toWxOpenOauth2?action=carDetail&state=%@&marketId=%@",WXContextApi,carId,[NRWCarUser sharedUser].marketId];

    }else{
        NSString *shopId =[NRWCarUser sharedUser].shopId ? [NRWCarUser sharedUser].shopId : @"";
        url = [NSString stringWithFormat:@"%@/controller/wx/toWxOpenOauth2?action=shopHome&state=%@&marketId=%@",WXContextApi,shopId,[NRWCarUser sharedUser].marketId];

    }
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText               = NO;
    WXMediaMessage *urlMessage = [WXMediaMessage message];
    urlMessage.title = self.shareTitle;//分享标题
    urlMessage.description = self.shareDesc;//分享描述
    if ([self.imageUrl isEqualToString:@""]) {
        [urlMessage setThumbImage:[UIImage imageNamed:@"LOGO"]];
    }else{
  
        [urlMessage setThumbImage:[self getImageFromURL:self.imageUrl]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
    }
    //创建多媒体对象
    WXWebpageObject *webObj = [WXWebpageObject object];
    webObj.webpageUrl = url;//分享链接
    urlMessage.mediaObject = webObj;
    req.message = urlMessage;
    // 目标场景
    // 发送到聊天界面  WXSceneSession
    // 发送到朋友圈    WXSceneTimeline
    // 发送到微信收藏  WXSceneFavorite
    if (tag == 0) {
        req.scene = WXSceneSession;
    }else{
        req.scene = WXSceneTimeline;
    }
    [WXApi sendReq:req];
}

-(UIImage *) getImageFromURL:(NSString *)fileURL
{
    
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    return result;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
